package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.databinding.WidgetPartyCoverMarqueeBinding
import com.polaris.live.net.liveService
import com.polaris.live.widget.marqueen.SimpleNoticeTextView

/**
 *
 * @author Created by 天晴 on 2024/7/2 14:41
 * @since 1.0.0
 **/
class PartyCoverMarqueeView(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
    val mBinding = WidgetPartyCoverMarqueeBinding.inflate(LayoutInflater.from(context), this)
    private val marqueeFactory: SimpleNoticeTextView

    init {
        marqueeFactory = SimpleNoticeTextView(context)
        mBinding.marquee.setMarqueeFactory(marqueeFactory)
    }

    fun getPartyCoverScroll(mViewModel: BaseViewModel) {
        request(mViewModel)
    }


    private fun request(mViewModel: BaseViewModel) {
        mViewModel.request({ liveService.partyRemind() }, {
            if (it.isNotEmpty()) {
                visibility = View.VISIBLE
            }
            marqueeFactory.setData(it)
            if (it.size > 1 && !mBinding.marquee.isFlipping) {
                mBinding.marquee.startFlipping()
            }
        })
    }


    fun reset() {
        if (mBinding.marquee.isFlipping) {
            mBinding.marquee.stopFlipping()
        }
        visibility = View.GONE
    }

}